import Tittle from "../Tittle";
import AddressItem from "../AddressItem";
import urlHook from "@page/common/utils/urlHook";

import HttpUtils from "@page/common/utils/HttpUtils";
import "./style.scss";

class OriginalInfo extends React.Component {
    constructor() {
        super();
        this.state = {
            addressList: []
        };
    }

    componentDidMount() {
        this.getDeliveryAddress();
    }
    
    getDeliveryAddress() {
        HttpUtils.getDeliveryAddress({})
            .then(res => {
                console.log(res);
                let index = 0;
                res.addressList &&
                    res.addressList.forEach((v, k) => {
                        v.select = v.default ? true : false;
                        index = v.default ? k : 0;
                        v.deliveryAdress = v.hasOwnProperty("label")&&v.label!=""&&v.remark==""?(v.deliveryAdress+"-"+v.label):v.hasOwnProperty("label")&&v.label!=""&&v.remark!=""?(v.deliveryAdress+"-"+v.label+"("+v.remark+")"):v.deliveryAdress;
                    });
                console.log("getDeliveryAddress",res.addressList)
                this.setState({
                    addressList: res.addressList
                });
                this.props.addressBc(res.addressList[index],'first');
            })
            .catch(err => {
                console.log(err);
            });
    }

    selectAddress=(index, list)=> {
        if (list) {
            list.forEach((v, k) => {
                v.select = index === k ? true : false;
            });
            this.setState({
                addressList: list
            });
            this.props.addressBc(list[index]);
        } else {
            this.props.addressBc("add");
        }
    }

    editAddress2=(id)=> {
        this.refs.AddressItem.showLabelModal(id);
    }

    //设置默认地址
    setDefaul=(index, list)=> {
        list.forEach((v, k) => {
            v.default = index === k ? true : false;
        });
        this.setState({
            addressList: list
        });
    }

    //
    jump = () => {
        window.open(urlHook.addressList, "_blank");
    };

    render() {
        const { addressList } = this.state;
        console.log("OriginalInfoRender",this.state);
        return (
            <div className="rj_original_info">
                <div className="rj_address-wp zen_ps-re">
                    <Tittle text="收货人信息" />
                    <div className={Uts.sc(['rj_wp-address-list rj_l-scroll',addressList&&addressList.length<=7?'rj_l_scroll-none':''])}>
                        <AddressItem
                            selectAddress={this.selectAddress}
                            setDefaul={this.setDefaul}
                            list = {addressList}
                            updateAddress = {this.getDeliveryAddress.bind(this)}
                            ref={"AddressItem"}
                            updateStateLabel={this.props.updateStateLabel}
                        />
                        <AddressItem
                            selectAddress={this.selectAddress.bind(this)}
                            isSelect={false}
                            isShowAdd={true}
                            list = {addressList}
                            updateAddress = {this.getDeliveryAddress.bind(this)}
                            ref={"AddressItem"}
                            updateStateLabel={this.props.updateStateLabel}
                        />
                    </div>
                   
                    <span onClick={this.jump} className="rj_l-address-man">管理收货地址 </span>
                </div>
            </div>
        );
    }
}

export default OriginalInfo;
